package com.jianganming.nio.reactor;

import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

//获取客户端连接
public class Acceptor implements Runnable {
    private final Selector selector;

    private final ServerSocketChannel serverSocketChannel;

    public Acceptor(Selector selector, ServerSocketChannel serverSocketChannel) {
        this.selector = selector;
        this.serverSocketChannel = serverSocketChannel;
    }

    @Override
    public void run() {
        try{
            SocketChannel channel = serverSocketChannel.accept();//获取客户端连接
            System.out.println(channel.getRemoteAddress()+":收到一个客户端连接");
            channel.configureBlocking(false);
            //channel.register(selector,SelectionKey.OP_READ,new Handler(channel));//单线程模式
            channel.register(selector,SelectionKey.OP_READ,new MutilDispatchHandler(channel));//多线程模式
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
